﻿using RpcClient;
using RpcModel;
using RpcModel.Tran;
using RpcModel.Tran.Model;
using RpcSync.Collect;
using RpcSync.Model;
using RpcSync.Service.Interface;

namespace RpcSync.Service.Event
{
    /// <summary>
    /// 远程事务模块
    /// </summary>
    internal class RpcTranEvent : RpcClient.Interface.IRpcApiService
    {
        private IRpcTransactionService _Service;
        private ITransactionCollect _Tran;
        public RpcTranEvent(IRpcTransactionService service, ITransactionCollect tran)
        {
            this._Tran = tran;
            this._Service = service;
        }


        /// <summary>
        /// 检查超时的事务
        /// </summary>
        public void CheckOverTimeTran()
        {
            _Service.CheckOverTimeTran();
        }
        /// <summary>
        /// 获取事务结果
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public TranResult GetTranResult(GetTranResult obj)
        {
            return _Service.GetTranResult(obj.TranId);
        }

        /// <summary>
        /// 重启失败的事务
        /// </summary>
        public void RestartRetryTran()
        {
            _Service.RestartRetryTran();
        }
        /// <summary>
        /// 锁事务超时
        /// </summary>
        public void TranLockOverTime()
        {
            _Service.TranLockOverTime();
        }
        /// <summary>
        /// 申请事务
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="source"></param>
        public void ApplyTran(ApplyTran obj, MsgSource source)
        {
            _Service.ApplyTransaction(source, obj);
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <param name="obj"></param>
        public void RollbackTran(RollbackTran obj)
        {
            _Service.RollbackTran(obj.TranId);
        }
        /// <summary>
        /// 设置事务的扩展参数
        /// </summary>
        /// <param name="obj"></param>
        public void SetTranExtend(SetTranExtend obj)
        {
            _Tran.SetTranExtend(obj.TranId, obj.Extend);
        }
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="obj"></param>
        public void SubmitTran(SubmitTran obj)
        {
            _Service.SubmitTran(obj.TranId);
        }
        /// <summary>
        /// 获取事务状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public RpcTranState GetTranState(GetTranState obj)
        {
            return _Tran.GetTranState(obj.TranId).ConvertMap<RegTranState, RpcTranState>();
        }


        /// <summary>
        /// 添加事务日志
        /// </summary>
        /// <param name="add"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public Guid AddTranLog(AddTranLog add, MsgSource source)
        {
            return _Service.AddTranLog(add.TranId, add.TranLog, source);
        }
    }
}
